package me.zhengjie.modules.houseshowconfig.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.houseshowconfig.domain.HouseShowConfig;
import me.zhengjie.modules.houseshowconfig.service.HouseShowConfigService;
import me.zhengjie.modules.houseshowconfig.service.dto.HouseShowConfigDto;
import me.zhengjie.modules.houseshowconfig.service.dto.HouseShowConfigQueryParam;
import me.zhengjie.modules.houseshowconfig.service.mapper.HouseShowConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2021-12-24
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = HouseShowConfigService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HouseShowConfigServiceImpl extends CommonServiceImpl<HouseShowConfigMapper, HouseShowConfig> implements HouseShowConfigService {

    // private final RedisUtils redisUtils;
    private final HouseShowConfigMapper houseShowConfigMapper;

    @Override
    public PageInfo<HouseShowConfigDto> queryAll(HouseShowConfigQueryParam query, Pageable pageable) {
        IPage<HouseShowConfig> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<HouseShowConfig> page = houseShowConfigMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, HouseShowConfigDto.class);
    }

    @Override
    public List<HouseShowConfigDto> queryAll(HouseShowConfigQueryParam query){
        return ConvertUtil.convertList(houseShowConfigMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), HouseShowConfigDto.class);
    }

    @Override
    public HouseShowConfig getById(Long id) {
        return houseShowConfigMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public HouseShowConfigDto findById(Long id) {
        return ConvertUtil.convert(getById(id), HouseShowConfigDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(HouseShowConfigDto resources) {
        HouseShowConfig entity = ConvertUtil.convert(resources, HouseShowConfig.class);
        return houseShowConfigMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(HouseShowConfigDto resources){
        HouseShowConfig entity = ConvertUtil.convert(resources, HouseShowConfig.class);
        int ret = houseShowConfigMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return houseShowConfigMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<HouseShowConfigDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (HouseShowConfigDto houseShowConfig : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("展示ID", houseShowConfig.getHouseShowId());
              map.put("场景类型：
panorama 全景信息
example 样板间
roam 园林漫步", houseShowConfig.getType());
              map.put("场景名称，当场景类型为example 样板间时必填，为户型名称", houseShowConfig.getName());
              map.put("场景详情内容，在场景底部展示，可空", houseShowConfig.getDetail());
              map.put("场景整体样式缩略图，在场景左上角展示，可空", houseShowConfig.getPreviewPanorama());
              map.put("第一个场景ID", houseShowConfig.getFirstScene());
              map.put("全景图场景切换时的持续时间,单位毫秒", houseShowConfig.getSceneFadeDuration());
              map.put("是否自动旋转，在加载之后，全景图会水平旋转显示，负数为往右边旋转，整数为往左边旋转，值为数字类型", houseShowConfig.getAutoRotate());
              map.put("在autoRotate设定的情况下，用户停止操作多长时间后继续自动旋转，单位为毫秒", houseShowConfig.getAutoRotateInactivityDelay());
              map.put("更新时间", houseShowConfig.getUpdateTime());
              map.put("创建时间", houseShowConfig.getCreateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
